#!/bin/bash

. $OPENARS_HOME/common/functions.h

rep_dir=$REPOSITORY_PATH/configs
if [ ! -d "$rep_dir" ]; then
    echo "not find repository dir"
    exit 1
fi

cd `dirname $0`
mkdir -p $OPENARS_HOME/logs/zabbix_manager $OPENARS_HOME/scripts/zabbix_manager/temp
MYLOG=$OPENARS_HOME/logs/zabbix_manager/install_zabbix_`date +%Y-%m-%d`.log
exec 2>>$MYLOG


list=`ls input/configs2install | awk -F. '$2 == "cfg" {print $1}'`
if [ -z "$list" ]; then
    echo "not find configs to install (use make_config.sh)"
    echo "path: input/configs2install"
    exit 1
fi

echo -e "\tservers:\n$list"
question "Continue?" qres
if [ "$qres" != "Y" ]; then
    echo "exit"
    exit 0;
fi

{
echo "======================================"
echo "`basename $0` `date +'%Y.%m.%d %H:%M'`"
echo "======================================"  
echo ""
echo "check servers ..."
for server in $list
do
echo "$server"
cks_rep=`cksum $rep_dir/$server/zabbix_agentd.conf |awk '{print $1}'`
cks_srv=`ssh $USER_ZABBIX@$server "cksum /etc/zabbix/zabbix_agentd.conf" |awk '{print $1}'`
cks_tmp=`cksum input/configs2install/$server.cfg |awk '{print $1}'`

[ -z "$cks_srv" ] && echo -e "\twarn: not get checksum config on server"
[ -z "$cks_rep" ] && echo -e "\twarn: not get checksum config on repository"
[ -z "$cks_tmp" ] && echo -e "\t err: not get checksum config on configs2install"
[ ! -z "$cks_rep" -a ! -z "$cks_srv" -a "$cks_rep" != "$cks_srv" ] && echo -e "\twarn: error config cksumm in repository or $server!"
[ ! -z "$cks_tmp" -a "$cks_tmp" == "$cks_srv" ] && echo -e "\twarn: new config equal config in server"
[ ! -z "$cks_tmp" -a "$cks_tmp" == "$cks_rep" ] && echo -e "\twarn: new config equal config in repository"
#echo "cks_rep: $cks_rep, cks_srv: $cks_srv, cks_tmp: $cks_tmp" 
done

question "Continue?" qres
if [ "$qres" != "Y" ]; then
    echo "exit"
    exit 0;
fi

> temp/srvOK.lst
for server in $list
do
echo $server
#if [ "$cks_tmp" != "$cks_serv" -o "$1" == "-N" ]; then
echo -en "\tcopy config..."
success_flag=
sftp $USER_ZABBIX@$server 1>temp/iconf_sftp.log 2>temp/iconf_sftp.err << END
put input/configs2install/$server.cfg /etc/zabbix/zabbix_agentd.conf 
END
    if [ `cat temp/iconf_sftp.err 2>/dev/null | grep -v "^|" | grep -v "^$" | wc -l` -gt 1 ]; then
        echo "ERR" 
    else
        echo "OK"
        success_flag="OK"
    fi
    cat temp/iconf_sftp.log temp/iconf_sftp.err >>$MYLOG
    rm -f temp/iconf_sftp.log temp/iconf_sftp.err
    if [ "$success_flag" = "OK" ]; then
	    echo $server >> temp/srvOK.lst 
	    echo -en "\tmove to repository ... "
	    mkdir -p $rep_dir/$server/
	    mv input/configs2install/$server.cfg $rep_dir/$server/zabbix_agentd.conf >>$MYLOG 2>&1
            if [ "$?" -eq 0 ]; then
		echo "OK"
	    else
		echo "ERR"
	    fi
	    if [ -d "$rep_dir/.svn" ]; then
		cd $rep_dir
		svn status $server | grep "?" >/dev/null
		[ "$?" -eq 0 ] && svn add $server
		cd - > /dev/null
	    fi
    else
	    echo -e "\tnot copy!"
    fi	
done

if [ -d "$rep_dir/.svn" ]; then
    echo -e "\nsvn commit ..."
    cd $rep_dir
    svn commit -m "`svn status`"
    sleep 1
    svn update
    cd - > /dev/null
fi

echo -e "\nrestart agents ... &"
} | tee -a $MYLOG

while read server
do
[ ! -z "$server" ] && ssh $USER_ZABBIX@$server "if [ \"\`ps -ef|grep restart.agent.sh|grep -v grep|wc -l\`\" -ge 1 ] ; then echo \"\`hostname\`: Zabbix restarter has already started\" ;else /etc/zabbix/restart.agent.sh; fi" &
done < temp/srvOK.lst
rm -f temp/srvOK.lst

exit 0

